package com.example.fitnessservice.api.web;

import com.example.fitnessservice.module.pojo.*;
import com.example.fitnessservice.service.BbsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/bbs")
public class BbsController {
    @Autowired
    private BbsService bbsService;
    //推荐动态
    @GetMapping("/recommendDynamic")
    public Result recommendDynamic(){
        List<UserDynamic> recommendDynamic = bbsService.recommendDynamic();

        Result result = new Result();
        if(recommendDynamic != null){
            result.setCode(200);
            result.setMsg("推荐动态查询成功");
            result.setData(recommendDynamic);
        }else {
            result.setCode(500);
            result.setMsg("推荐动态查询失败");
        }

        return result;
    }

    //查看动态内容
    @GetMapping("/dynamicContent")
    public Result dynamicContent(Integer dynamicId){
        UserDynamic userDynamic = bbsService.dynamicContent(dynamicId);
        Result result = new Result();
        if(userDynamic != null){
            result.setCode(200);
            result.setMsg("动态内容查询成功");
            result.setData(userDynamic);
        }else {
            result.setCode(500);
            result.setMsg("动态内容查询失败");
        }

        return result;
    }

    //查看个人资料（用户信息）
    @GetMapping("/userInfo")
    public Result findUserInfo(Integer userId){
        UserInfo userInfo = bbsService.findUserInfo(userId);
        Result result = new Result();
        if(userInfo != null){
            result.setCode(200);
            result.setMsg("用户信息查询成功");
            result.setData(userInfo);
        }else {
            result.setCode(500);
            result.setMsg("用户信息查询失败");
        }

        return result;
    }

    //查看用户粉丝列表
    @GetMapping("/findAllFans")
    public Result findAllFans(Integer userId){
        List<UserDynamicFans> allFans = bbsService.findAllFans(userId);
        Result result = new Result();
        if(allFans != null){
            result.setCode(200);
            result.setMsg("用户粉丝列表查询成功");
            result.setData(allFans);
        }else {
            result.setCode(500);
            result.setMsg("用户粉丝列表查询失败");
        }

        return result;
    }

    //查看用户关注列表
    @GetMapping("/findAllAttention")
    public Result findAllAttention(Integer userId){
        List<UserDynamicAttention> allAttention = bbsService.findAllAttention(userId);
        Result result = new Result();
        if(allAttention != null){
            result.setCode(200);
            result.setMsg("用户关注列表查询成功");
            result.setData(allAttention);
        }else {
            result.setCode(500);
            result.setMsg("用户关注列表查询失败");
        }

        return result;
    }

    //查看用户粉丝数
    @GetMapping("/findFansNum")
    public Result findFansNum(Integer userId){
        int count = bbsService.findFansNum(userId);
        Result result = new Result();
        if(count != 0){
            result.setCode(200);
            result.setMsg("用户粉丝数查询成功");
            result.setData(count);
        }else {
            result.setCode(500);
            result.setMsg("用户粉丝数查询失败");
        }
        return result;
    }

    //查看用户关注数
    @GetMapping("/findAttentionNum")
    public Result findAttentionNum(Integer userId){
        int count = bbsService.findAttentionNum(userId);
        Result result = new Result();
        if(count != 0){
            result.setCode(200);
            result.setMsg("用户关注数查询成功");
            result.setData(count);
        }else {
            result.setCode(500);
            result.setMsg("用户关注数查询失败");
        }
        return result;
    }

    //关注发布者（添加关注,也包括成为发布者粉丝）
    @PostMapping("/addAttention")
    public Result addAttention(@RequestBody UserDynamicAttention userDynamicAttention){
        boolean flag = bbsService.addAttention(userDynamicAttention);
        Result result = new Result();
        if(flag){
            result.setCode(200);
            result.setMsg("添加关注成功");
        }else {
            result.setCode(500);
            result.setMsg("添加关注失败");
        }
        return result;
    }

    //取消关注,也取消成为发布者粉丝
    @DeleteMapping("/deleteAttention")
    public Result deleteAttention(@RequestBody UserDynamicAttention userDynamicAttention){
        boolean flag = bbsService.deleteAttention(userDynamicAttention);
        Result result = new Result();
        if(flag){
            result.setCode(200);
            result.setMsg("取消关注成功");
        }else {
            result.setCode(500);
            result.setMsg("取消关注失败");
        }
        return result;
    }

    //用户的动态列表展示
    //改
    @GetMapping("/findDynamicByUserId")
    public Result findDynamicByUserId(HttpServletRequest request){
        //Object userId = request.getSession().getAttribute("userId");
        List<UserDynamic> userDynamics = bbsService.findDynamicByUserId(2);

        Result result = new Result();
        if(userDynamics != null){
            result.setCode(200);
            result.setMsg("用户的动态列表查询成功");
            result.setData(userDynamics);
        }else {
            result.setCode(500);
            result.setMsg("用户的动态列表查询失败");
        }

        return result;
    }

    //点赞动态
    @GetMapping("/plusLike")
    public Result plusLike(Integer dynamicId){
        System.out.println(dynamicId);
        boolean flag = bbsService.plusLike(dynamicId);
        Result result = new Result();
        if(flag){
            result.setCode(200);
            result.setMsg("点赞成功");
        }else {
            result.setCode(500);
            result.setMsg("点赞失败");
        }
        return result;
    }

    //取消点赞
    @PutMapping("/subtractLike")
    public Result subtractLike(Integer dynamicId){
        boolean flag = bbsService.subtractLike(dynamicId);
        Result result = new Result();
        if(flag){
            result.setCode(200);
            result.setMsg("取消点赞成功");
        }else {
            result.setCode(500);
            result.setMsg("取消点赞失败");
        }
        return result;
    }

    //评论动态(添加评论)
    @PostMapping("/addDynamicComment")
    public Result addDynamicComment(@RequestBody UserDynamicComment userDynamicComment){
        boolean flag = bbsService.addDynamicComment(userDynamicComment);
        Result result = new Result();
        if(flag){
            result.setCode(200);
            result.setMsg("评论成功");
        }else {
            result.setCode(500);
            result.setMsg("评论含敏感词，请修改");
        }
        return result;
    }

    //查看评论
    @GetMapping("/findDynamicComment")
    public Result findDynamicComment(Integer dynamicId){
        List<UserDynamicComment> userDynamicComments = bbsService.findDynamicComment(dynamicId);
        Result result = new Result();
        if(userDynamicComments != null){
            result.setCode(200);
            result.setMsg("评论查看成功");
            result.setData(userDynamicComments);
        }else {
            result.setCode(500);
            result.setMsg("评论查看失败");
        }
        return result;
    }

    //删除评论
    @DeleteMapping("/deleteDynamicComment")
    public Result deleteDynamicComment(@RequestBody UserDynamicComment userDynamicComment){
        boolean flag = bbsService.deleteDynamicComment(userDynamicComment);
        Result result = new Result();
        if(flag){
            result.setCode(200);
            result.setMsg("评论删除成功");
        }else {
            result.setCode(500);
            result.setMsg("评论删除失败");
        }
        return result;
    }

    //转发动态
    @GetMapping("/forwardingDynamic")
    public Result forwardingDynamic(HttpServletRequest request){
        StringBuffer requestURL = request.getRequestURL();
        Result result = new Result();
        if(requestURL != null){
            result.setCode(200);
            result.setMsg("请复制url进行转发");
            result.setData(requestURL);
        }else {
            result.setCode(500);
            result.setMsg("转发失败");
        }
        return result;
    }

    //查询举报内容/类型，选择举报原因
    @GetMapping("/findReportContent")
    public Result findReportContent(){
        List<UserDynamicReport> userDynamicReport = bbsService.findReportContent();
        Result result = new Result();
        if(userDynamicReport != null){
            result.setCode(200);
            result.setMsg("举报类型查询成功");
            result.setData(userDynamicReport);
        }else {
            result.setCode(500);
            result.setMsg("举报类型查询失败");
        }
        return result;
    }

    //提交举报
    @PutMapping("/submitReport")
    public Result submitReport(Integer reportId, Integer dynamicId){
        boolean flag = bbsService.submitReport(reportId,dynamicId);
        Result result = new Result();
        if(flag){
            result.setCode(200);
            result.setMsg("提交举报成功");
        }else {
            result.setCode(500);
            result.setMsg("提交举报失败");
        }
        return result;
    }

    //关注动态，展示我关注的发布者发布的动态，(以时间顺序展示,待扩展)
    //改
    @GetMapping("/findAttentionDynamic")
    public Result findAttentionDynamic(HttpServletRequest request){
        //Object userId = request.getSession().getAttribute("userId");
        List<UserDynamic> allAttentionDynamics = bbsService.findAttentionDynamic(2);
        System.out.println("查询成功");
        Result result = new Result();
        if(allAttentionDynamics != null){
            result.setCode(200);
            result.setMsg("关注动态查询成功");
            result.setData(allAttentionDynamics);
        }else {
            result.setCode(500);
            result.setMsg("暂无关注者发布动态");
        }

        return result;
    }

    //发布动态(还有问题待解决)
/*    @PostMapping("/dynamicIssue")
    public Result dynamicIssue(@RequestBody UserDynamicContent userDynamicContent){
        int count = bbsService.dynamicIssue(userDynamicContent);
        Result result = new Result();
        if(count == 0){
            result.setCode(100);
            result.setMsg("动态审核中");
        }else if(count == 1){
            result.setCode(200);
            result.setMsg("动态审核通过");
        }else if(count == 2){
            result.setCode(300);
            result.setMsg("动态申诉中");
        }else if(count == -1){
            result.setCode(400);
            result.setMsg("机器敏感词识别未通过审核通过");
        }else if(count == -2){
            result.setCode(500);
            result.setMsg("管理员审核未通过");
        }
        return result;
    }*/

    //删除动态
    //改
    @DeleteMapping("/deleteDynamic")
    public Result deleteDynamic(Integer dynamicId){
        boolean flag = bbsService.deleteDynamic(dynamicId);
        Result result = new Result();
        if(flag){
            result.setCode(200);
            result.setMsg("动态删除成功");
        }else {
            result.setCode(500);
            result.setMsg("动态删除失败");
        }
        return result;
    }

    //编辑个人资料
    @PostMapping("/addUserInfo")
    public Result addUserInfo(@RequestBody UserInfo userInfo){
        boolean flag = bbsService.addUserInfo(userInfo);
        Result result = new Result();
        if(flag){
            result.setCode(200);
            result.setMsg("编辑成功");
        }else {
            result.setCode(500);
            result.setMsg("编辑失败");
        }
        return result;
    }

    //关注动态，展示我关注的发布者发布的动态的图片，(以时间顺序展示,待扩展)
    //改
    @GetMapping("/findAttentionDynamicImg")
    public Result findAttentionDynamicImg(HttpServletRequest request){
        //Object userId = request.getSession().getAttribute("userId");
        List<String> findAttentionDynamicImg = bbsService.findAttentionDynamicImg(2);
        System.out.println("查询成功");
        Result result = new Result();
        if(findAttentionDynamicImg != null){
            result.setCode(200);
            result.setMsg("关注动态查询成功");
            result.setData(findAttentionDynamicImg);
        }else {
            result.setCode(500);
            result.setMsg("暂无关注者发布动态");
        }

        return result;
    }
}
